import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { api } from '@/lib/apiService';

export async function GET(request) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session?.user?.email) {
      return NextResponse.json({ error: '未授权' }, { status: 401 });
    }
    
    // 调用Python后端的获取标签API - 使用带认证的方法
    const response = await api.auth.get('tags');
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Error fetching tags:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}

export async function POST(request) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session?.user?.email) {
      return NextResponse.json({ error: '未授权' }, { status: 401 });
    }

    const { name } = await request.json();

    if (!name) {
      return NextResponse.json({ error: 'Tag name is required' }, { status: 400 });
    }
    
    // 调用Python后端的创建标签API - 使用带认证的方法
    const response = await api.auth.post('tags', { name });
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Error creating tag:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}

export async function DELETE(request) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session?.user?.email) {
      return NextResponse.json({ error: '未授权' }, { status: 401 });
    }
    
    // 获取URL参数中的tag ID
    const url = new URL(request.url);
    const tagId = url.searchParams.get('id');
    
    if (!tagId) {
      return NextResponse.json({ error: 'Tag ID is required' }, { status: 400 });
    }
    
    // 调用Python后端的删除标签API - 使用带认证的方法
    const response = await api.auth.delete(`tags?id=${tagId}`);
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Error deleting tag:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}

export async function PATCH(request) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session?.user?.email) {
      return NextResponse.json({ error: '未授权' }, { status: 401 });
    }
    
    // 获取URL参数中的tag ID
    const url = new URL(request.url);
    const tagId = url.searchParams.get('id');
    
    if (!tagId) {
      return NextResponse.json({ error: 'Tag ID is required' }, { status: 400 });
    }
    
    const { name } = await request.json();
    
    if (!name) {
      return NextResponse.json({ error: 'Tag name is required' }, { status: 400 });
    }
    
    // 调用Python后端的更新标签API - 使用带认证的方法
    const response = await api.auth.patch(`tags?id=${tagId}`, { name });
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Error updating tag:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}
